<?php

$data = $model->search()->data;

$res = array();
foreach($data as $d) {
    $floors = $d->floors;
    $floor = Floor::model();
    $floor->building_id = count($floors) > 0 ? $floors[0]->building_id : "fail";
    $editBtn = CHtml::button("Edit", array("onclick" => "building_openForUpdate(".$d->id.",'".$d->name."','buildingAccordion')"));

    //Generate javascript ajax delete function
    $jsDelFunction = '
        openConfirmationModalDialog(
            \'Are you sure you want to delete <b>'. $d->name .'</b>?\',
            function(){
                $.getJSON(\''
                    .$this->createUrl("building/delete", array("id" => $d->id)).'\',
                    function(data) {
                        if(data.errId) {

                        } else {
                            closeConfirmationModalDialog();
                            $.ajax({
                                url: \''.$this->createUrl($this->id . '/getaccordion').'\',
                                success: function(data, textStatus, jqXHR) {
                                    $(\'#buildingAccordion\').html(data);
                                    $(\'#accordion\').accordion();
                                    MAP.refresh();
                                }
                            });
                        }
                    }
                );
            }
        )
    ';
    $delBtn = CHtml::button("Delete", array("onclick" => $jsDelFunction));
    $res[$d->name . " " . $editBtn . $delBtn] = $this->renderPartial("_floor", array("model" => $floor, "building_id" => $d->id), true);
}

$this->widget('zii.widgets.jui.CJuiAccordion', array(
        'id' => 'accordion',
        'panels'=>$res
    )
);

?>
